home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7570 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to get a random s
  5. Date: 27 Feb 1996 11:19:52 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4gupco$q9r@gail.ripco.com>
  8. NNTP-Posting-Host: foley.ripco.com
  9.  
  10.  
  11. chancl@nevada.edu (Clapton Chan) asked, among other things, about
  12. seeding rand.
  13.  
  14. After posting a correct and legal answer (snipped away by Alan Bowler),
  15. I ended that article with
  16.  
  17. > [A poor practice follows]
  18. > if you do not #include <time.h>, you need
  19. >     srand((unsigned)time(NULL));
  20.  
  21. Mr. Bowler, who apparently did not understand `a poor practice' as
  22. strong condemnation, inserted the hyperbole
  23.  
  24. > Worse than poor.
  25.  
  26. It seems that Mr. Bowler condones poor programming practice and condemns
  27. only the `worse than poor'. [Insert your own damn smilies.]  Poor
  28. practices are to avoided; this one is unfortunately very common.
  29.  
  30. In any case, Mr. Bowler pointed out
  31. > Adding the cast to unsigned will not change that fact that
  32. > this implicit declaration means your code could be looking in the
  33. > wrong place.
  34.  
  35. The `wrong place' argument -- and discussion about registers for
  36. different machines -- is not needed for to condemn this code.
  37.  
  38. gumboot@airdmhor.gen.nz (Simon Hosie) in <4gqir9$d5r@airdmhor.gen.nz>
  39. now asks:
  40.  
  41. >  I don't follow that.. if time returns a float then won't the result be
  42. >cast and truncated to an int?
  43.  
  44. The problem is that, without including a declaration for time(), which
  45. is the point of the `#include <time.h>', the compiler has no way of
  46. knowing that time() returns anything other than an int.
  47.  
  48. In fact, it will assume that time() _does_ return an int.  It will not
  49. cast it from whatever a time_t is to an integer type.  The explicit cast
  50. converts the presumed int return value (even if a floating type was
  51. returned) to an unsigned int.  This is not very helpful when time_t is a
  52. floating point type.
  53.  
  54. Even if the same registers are used for time_t and int values, if time_t
  55. is not an int, then the value time() returns is not the value that the
  56. caller gets.  The same bit pattern will be interpreted differently.
  57.  
  58. Mr. Bowler is taking a different tack on this.  His point is that time()
  59. may return its value in a different register (e.g. floating point reg)
  60. from that the caller expects (an integer reg).  In that case, the value
  61. time() returns will not never even be seen by the caller.
  62.                                                                 
  63. --
  64. * Martin Ambuhl       net: mambuhl@ripco.com
  65. * Chicago, IL (USA)    
  66.